﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using EngineOfEvermore.Scene;
using EngineOfEvermore.Common;
using NUnit.Framework;
using System.Diagnostics;
using EngineOfEvermore.Editor.Common.Command;
using EngineOfEvermore.Imaging;

namespace EngineOfEvermore.Editor.SceneTools
{
    interface IObjectMover
    {
        bool IsCurrentlyMovingObjects { get; }
        IEnumerable<SceneObject> CurrentObjects { get; }

        void BeginMovingObjects( IEnumerable<SceneObject> sceneObjects, 
            Point mousePosition );
        void UpdateObjectPositions( Point mousePosition );
        void FinishMovingObjects();
    }

    class ObjectMover : IObjectMover
    {
        #region Properties
        public ICommandStack CommandStack
        {
            get;
            private set;
        }

        public IReadonlyCamera Camera
        {
            get;
            private set;
        }

        public bool IsCurrentlyMovingObjects
        {
            get;
            private set;
        }

        public IEnumerable<SceneObject> CurrentObjects
        {
            get;
            private set;
        }

        public Vector2d ReferencePoint
        {
            get;
            private set;
        }

        public Vector2d InitialReferencePoint
        {
            get;
            private set;
        }
        #endregion

        #region Constructor
        public ObjectMover( ICommandStack commandStack, IReadonlyCamera camera )
        {
            Debug.Assert( commandStack != null );
            Debug.Assert( camera != null );

            this.CommandStack = commandStack;
            this.Camera = camera;
        }
        #endregion

        #region Methods
        public void BeginMovingObjects( IEnumerable<SceneObject> sceneObjects, 
            Point mousePosition )
        {
            Debug.Assert( sceneObjects != null && sceneObjects.Any() );
            Debug.Assert( !this.IsCurrentlyMovingObjects );
            
            this.CurrentObjects = sceneObjects.ToArray();
            this.IsCurrentlyMovingObjects = true;
            this.ReferencePoint = 
                this.Camera.ToWorld( new Vector2d( mousePosition.X, mousePosition.Y ) );

            this.InitialReferencePoint = this.ReferencePoint;
        }

        public void UpdateObjectPositions( Point mousePosition )
        {
            Debug.Assert( this.IsCurrentlyMovingObjects );
            
            Vector2d position = this.Camera.ToWorld( 
                new Vector2d( mousePosition.X, mousePosition.Y ) );

            Vector2d offset = position - this.ReferencePoint;
            
            foreach ( var sceneObject in this.CurrentObjects )
            {
                sceneObject.Position += offset;
            }

            this.ReferencePoint = position;
        }

        public void FinishMovingObjects()
        {
            Debug.Assert( this.IsCurrentlyMovingObjects );

            var command = new MoveSceneObjectsCommand( this.CurrentObjects, 
                this.InitialReferencePoint, this.ReferencePoint );

            this.CommandStack.PushCommand( command );

            this.IsCurrentlyMovingObjects = false;
            this.ReferencePoint = Vector2d.Zero;
            this.InitialReferencePoint = Vector2d.Zero;
            this.CurrentObjects = Enumerable.Empty<SceneObject>();
        }
        #endregion

        #if ENGINE_OF_EVERMORE_ENABLE_UNIT_TESTS
        [TestFixture]
        public class Test
        {
            private MockCommandStack _commandStack;

            [Test]
            public void TestMovingObjects()
            {
                var referencePoint = new Vector2d( 3, 4 );
                var sceneObjects = Util.Generate( () => new SceneObject(), 2 ).ToArray();
                ObjectMover objectMover = _createObjectMover();

                objectMover.BeginMovingObjects( sceneObjects, (Point) referencePoint );

                Assert.That( objectMover.IsCurrentlyMovingObjects );
                Assert.That( objectMover.CurrentObjects, Is.EquivalentTo( sceneObjects ) );
                Assert.That( objectMover.ReferencePoint, Is.EqualTo( referencePoint ) );
                Assert.That( objectMover.InitialReferencePoint, Is.EqualTo( referencePoint ) );
            }

            [Test]
            public void TestUpdateObjectPositions()
            {
                var sceneObjects = Util.Generate( () => new SceneObject(), 2 ).ToArray();

                sceneObjects[0].Position = new Vector2d( 1, 0 );
                sceneObjects[1].Position = new Vector2d( 3, 2 );

                var initialRefPoint = new Vector2d( 3, 6 );
                var offset = new Vector2d( 2, 3 );
                var expectedPositions = new Vector2d[] 
                    {
                        sceneObjects[0].Position + (Vector2d) offset,
                        sceneObjects[1].Position + (Vector2d) offset
                    };

                ObjectMover objectMover = _createObjectMover();
                objectMover.BeginMovingObjects( sceneObjects, (Point) initialRefPoint );

                objectMover.UpdateObjectPositions( (Point) ( initialRefPoint + offset ) );

                Assert.That( objectMover.ReferencePoint, Is.EqualTo( offset + initialRefPoint ) );
                Assert.That( objectMover.InitialReferencePoint, Is.EqualTo( initialRefPoint ) );
                Assert.That( sceneObjects[0].Position, Is.EqualTo( expectedPositions[0] ) );
                Assert.That( sceneObjects[1].Position, Is.EqualTo( expectedPositions[1] ) );
                Assert.That( objectMover.IsCurrentlyMovingObjects );
                Assert.That( objectMover.CurrentObjects, Is.EquivalentTo( sceneObjects ) );
            }

            [Test]
            public void TestUpdateObjectPositionsTwice()
            {
                var sceneObjects = Util.Generate( () => new SceneObject(), 2 ).ToArray();

                sceneObjects[0].Position = new Vector2d( 1, 0 );
                sceneObjects[1].Position = new Vector2d( 3, 2 );

                var initialRefPoint = new Vector2d( 3, 6 );
                var offset1 = new Vector2d( 2, 3 );
                var offset2 = new Vector2d( -4, 8 );

                var expectedPositions = new Vector2d[] 
                    {
                        sceneObjects[0].Position + offset1 + offset2,
                        sceneObjects[1].Position + offset1 + offset2
                    };

                ObjectMover objectMover = _createObjectMover();
                objectMover.BeginMovingObjects( sceneObjects, (Point) initialRefPoint );

                objectMover.UpdateObjectPositions( (Point) ( initialRefPoint + offset1 ) );
                objectMover.UpdateObjectPositions( (Point) (initialRefPoint + offset1 + offset2 ) );

                Assert.That( objectMover.ReferencePoint, 
                    Is.EqualTo( initialRefPoint + offset1 + offset2 ) );
                Assert.That( objectMover.InitialReferencePoint, Is.EqualTo( initialRefPoint ) );

                Assert.That( sceneObjects[0].Position, Is.EqualTo( expectedPositions[0] ) );
                Assert.That( sceneObjects[1].Position, Is.EqualTo( expectedPositions[1] ) );
                Assert.That( objectMover.IsCurrentlyMovingObjects );
                Assert.That( objectMover.CurrentObjects, Is.EquivalentTo( sceneObjects ) );
            }

            [Test]
            public void TestFinishMovingObjects()
            {
                var sceneObjects = Util.Generate( () => new SceneObject(), 2 ).ToArray();

                sceneObjects[0].Position = new Vector2d( 1, 0 );
                sceneObjects[1].Position = new Vector2d( 3, 2 );

                var initialRefPoint = new Vector2d( 3, 6 );
                var offset = new Vector2d( 2, 3 );

                ObjectMover objectMover = _createObjectMover();

                objectMover.BeginMovingObjects( sceneObjects, (Point) initialRefPoint );
                objectMover.UpdateObjectPositions( (Point) ( initialRefPoint + offset ) );
                objectMover.FinishMovingObjects();

                Assert.That( objectMover.ReferencePoint, Is.EqualTo( Vector2d.Zero ) );
                Assert.That( objectMover.InitialReferencePoint, Is.EqualTo( Vector2d.Zero ) );
                Assert.That( objectMover.IsCurrentlyMovingObjects, Is.False );
                Assert.That( objectMover.CurrentObjects, Is.Empty );

                Assert.That( _commandStack.Commands.Count, Is.EqualTo( 1 ) );
                Assert.That( _commandStack.Commands.Peek() is MoveSceneObjectsCommand );

                var moveCommand = (MoveSceneObjectsCommand) _commandStack.Commands.Peek();
                Assert.That( moveCommand.From, Is.EqualTo( initialRefPoint ) );
                Assert.That( moveCommand.To, Is.EqualTo( initialRefPoint + offset ) );
                Assert.That( moveCommand.SceneObjects, Is.EquivalentTo( sceneObjects ) );
            }

            private ObjectMover _createObjectMover()
            {
                Camera camera = new Camera();
                camera.ViewportSize = new Size( 100, 100 );
                camera.Position = new Vector2d( 50, 50 );
                camera.Scale = 1;

                _commandStack = new MockCommandStack();
                return new ObjectMover( _commandStack, camera );
            }
        }
        #endif
    }
}
